home *** CD-ROM | disk | FTP | other *** search
- /*
- * (c) Copyright 1992 by Panagiotis Tsirigotis
- * All rights reserved. The file named COPYRIGHT specifies the terms
- * and conditions for redistribution.
- */
-
- static char RCSid[] = "$Id: logctl.c,v 5.1 1992/11/01 00:01:21 panos Exp $" ;
-
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <syslog.h>
- #include <fcntl.h>
-
- #include "sio.h"
- #include "xlog.h"
-
- #include "config.h"
- #include "defs.h"
- #include "log.h"
- #include "service.h"
- #include "state.h"
-
- void msg() ;
-
-
- /*
- * NOTE: This function uses the current configuration to determine the
- * common log file
- */
- status_e start_log( sp )
- struct service *sp ;
- {
- struct service_data *sdp = SDATA( sp ) ;
- struct service_config *scp = CONF( sp ) ;
- struct log *lp = LOG( scp ) ;
- xlog_h xh ;
- char *func = "start_log" ;
- xlog_h start_filelog() ;
- void log_in_error() ;
-
- switch ( lp->log_type )
- {
- case L_NONE:
- xh = NULL ;
-
- case L_SYSLOG:
- xh = xlog_create( XLOG_SYSLOG, scp->id, XLOG_NOFLAGS,
- SYSLOG( lp )->facility, SYSLOG( lp )->level ) ;
- if ( xh == NULL )
- {
- msg( LOG_ERR, func,
- "failed to create a log for service %s", scp->id ) ;
- return( FAILED ) ;
- }
- xlog_control( xh, XLOG_CALLBACK, log_in_error, (void *)sp ) ;
- break ;
-
- case L_FILE:
- /*
- * NOTE: if the same file is specified for more than one
- * service, it will be opened as many times
- * Furthermore, size control will not be accurate.
- */
- xh = start_filelog( scp->id, XLOG_NOFLAGS, FILELOG( lp ) ) ;
- if ( xh == NULL )
- return( FAILED ) ;
- xlog_control( xh, XLOG_CALLBACK, log_in_error, (void *)sp ) ;
- break ;
-
- case L_COMMON_FILE:
- if ( DEFAULT_LOG( ps ) == NULL )
- if ( DEFAULT_LOG_ERROR( ps ) )
- return( FAILED ) ;
- else
- {
- xh = start_filelog( "default", XLOG_NOFLAGS,
- FILELOG( LOG( DEFAULTS( ps ) ) ) ) ;
- if ( xh == NULL )
- {
- DEFAULT_LOG_ERROR( ps ) = TRUE ;
- return( FAILED ) ;
- }
- DEFAULT_LOG( ps ) = xh ;
- xlog_control( xh, XLOG_CALLBACK, log_in_error, VOID_NULL ) ;
- }
- else
- xh = DEFAULT_LOG( ps ) ;
- break ;
-
- default: /* SHOULDN'T HAPPEN */
- msg( LOG_ERR, func, "bad log type (%d) for service %s",
- (int) lp->log_type, scp->id ) ;
- return( FAILED ) ;
- }
- sdp->log_handle = xh ;
- return( OK ) ;
- }
-
-
- PRIVATE xlog_h start_filelog( id, flags, flp )
- char *id ;
- int flags ;
- struct filelog *flp ;
- {
- xlog_h xh ;
- int fd ;
- int log_file_mode = ( debug.on ) ? 0644 : LOG_FILE_MODE ;
- char *func = "start_filelog" ;
-
- xh = xlog_create( XLOG_FILELOG, id, flags,
- flp->filename, LOG_OPEN_FLAGS, log_file_mode ) ;
- if ( xh == NULL )
- {
- msg( LOG_ERR, func, "creation of %s log failed", id ) ;
- return( NULL ) ;
- }
-
- xlog_control( xh, XLOG_GETFD, &fd ) ;
- if ( fcntl( fd, F_SETFD, 1 ) == -1 )
- {
- msg( LOG_ERR, func, "fcntl F_SETFD: %m" ) ;
- xlog_destroy( xh ) ;
- return( NULL ) ;
- }
-
- ps.rws.descriptors_free-- ;
-
- if ( FILELOG_SIZE_CONTROL( flp ) )
- xlog_control( xh, XLOG_LIMITS, flp->soft_limit, flp->hard_limit ) ;
-
- return( xh ) ;
- }
-
-
- /*
- * The only purpose of this function is to enter a log message about
- * the xlog error.
- *
- * NOTE: We could destroy the xlog at this point but we choose not to.
- */
- PRIVATE void log_in_error( xh, error_code, arg )
- xlog_h xh ;
- int error_code ;
- void *arg ;
- {
- struct service *sp = SP( arg ) ;
- char *log_id = ( sp == NULL ) ? "common" : CONF( sp )->id ;
- char *func = "log_in_error" ;
-
- #ifdef lint
- xh = xh ;
- #endif
- if ( error_code == XLOG_ESIZE )
- msg( LOG_ERR, func, "Size of %s log exceeded hard limit", log_id ) ;
- else
- msg( LOG_ERR, func, "Error in %s log: %d", log_id, error_code ) ;
- }
-
-
- void end_log( type, xh )
- logtype_e type ;
- xlog_h xh ;
- {
- char *func = "end_log" ;
-
- if ( xh == NULL ) /* shouldn't be NULL but just in case */
- {
- msg( LOG_NOTICE, func, "end_log called with NULL handle" ) ;
- return ;
- }
-
- switch ( type )
- {
- case L_FILE:
- ps.rws.descriptors_free-- ;
- /* FALL THROUGH */
-
- case L_SYSLOG:
- xlog_destroy( xh ) ;
- }
- }
-
-